package com.macro.mall.controller;

import com.macro.mall.common.api.CommonPage;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.dto.PmsProductAttributeParam;
import com.macro.mall.model.PmsProductAttribute;
import com.macro.mall.service.PmsProductAttributeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/productAttribute")
@Api(tags = "PmsProductAttributeController",description = "商品属性管理")
public class PmsProductAttributeController {
    @Autowired
    private PmsProductAttributeService pmsProductAttributeService;
    @ApiOperation("根据id查询商品属性")
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public CommonResult<PmsProductAttributeParam> selectById(@PathVariable("id") Long id){
        PmsProductAttribute pmsProductAttribute = new PmsProductAttribute();
        PmsProductAttributeParam pmsProductAttributeParam = new PmsProductAttributeParam();
        BeanUtils.copyProperties(pmsProductAttribute,pmsProductAttributeParam);
        return CommonResult.success(pmsProductAttributeParam);
   }
    @ApiOperation("根据商品分类id查询商品属性和属性分类")
    @RequestMapping(value = "/attrInfo/{productCategoryId}",method = RequestMethod.GET)
    public CommonResult<Map<String,Long>> attrInfo(@PathVariable("productCategoryId") Long productCategoryId){
        PmsProductAttribute p1  = pmsProductAttributeService.selectProductByCategoryId(productCategoryId);
        Long Id = p1.getId();
        Long ProductAttributeCategoryId = p1.getProductAttributeCategoryId();
        Map<String,Long> map = new HashMap<>();
        map.put("attributeCategoryId",ProductAttributeCategoryId);
        map.put("attributeId",Id);
        return CommonResult.success(map);
    }
    @ApiOperation("添加商品属性信息")
    @RequestMapping(value = "/create",method = RequestMethod.POST)
    public CommonResult create(@RequestBody PmsProductAttributeParam pmsProductAttributeParam){
        int a = pmsProductAttributeService.insertPmsProductAttribute(pmsProductAttributeParam);
        if (a==1){
           return CommonResult.success(a);
        }else {
           return CommonResult.failed();
        }
    }
    @ApiOperation("根据id修改商品属性信息")
    @RequestMapping(value = "/update/{id}",method = RequestMethod.POST)
    public CommonResult update(@PathVariable("id") Long id,@RequestBody PmsProductAttributeParam pmsProductAttributeParam){
        pmsProductAttributeParam.setId(id);
        int a = pmsProductAttributeService.insertPmsProductAttribute(pmsProductAttributeParam);
        if (a==1){
            return CommonResult.success(a);
        }else {
            return CommonResult.failed();
        }
    }
    @ApiOperation("根据分类id查询商品属性信息和参数列表")
    @RequestMapping(value = "/list/{cid}",method = RequestMethod.GET)
    public CommonResult<CommonPage<PmsProductAttribute>> list(@PathVariable("cid") Long cid,Integer type,
                                                              @RequestParam(value = "pageNum",defaultValue = "1")
                                                              @ApiParam("页码") Integer pageNum,
                                                              @RequestParam(value = "pageSize",defaultValue = "4")
                                                              @ApiParam("每页数量") Integer pageSize){
          if (type==0){
              List<PmsProductAttribute> list = pmsProductAttributeService.selectPmsProductAttributeByCid(cid,pageNum,pageSize);
              return CommonResult.success(CommonPage.restPage(list));
          }else if(type==1) {
              List<PmsProductAttribute> list1 = pmsProductAttributeService.selectPmsProductValueByCid(cid,pageNum,pageSize);
              return CommonResult.success(CommonPage.restPage(list1));
          }
          return CommonResult.failed();
    }

    @ApiOperation("批量删除商品属性")
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public CommonResult deleteAll(@RequestBody List<Long> ids){
        int a = pmsProductAttributeService.deleteAll(ids);
        CommonResult commonResult;
        if (a == 1){
            commonResult = CommonResult.success(ids);
        }else {
            commonResult = CommonResult.failed("删除失败");
        }
        return commonResult;
    }
}
